% [P,var_list] = struct_subset(P,l_use,dim)

function [P,var_list] = struct_subset(P,l_use,dim)

    var_list = fieldnames(P);
    
    for var = 1:numel(var_list)
        try
            eval(['P.',var_list{var},' = CDC_subset(P.',var_list{var},',dim,l_use);']);
        catch
            disp([var_list{var},' is not subset'])
        end
    end
end

function output = CDC_subset(field,dim,list)
        
    switch dim,
        case 1,
            output = field(list,:,:,:,:,:,:);
        case 2,
            output = field(:,list,:,:,:,:,:);
        case 3,
            output = field(:,:,list,:,:,:,:);
        case 4,
            output = field(:,:,:,list,:,:,:);
        case 5,
            output = field(:,:,:,:,list,:,:);            
        case 6,
            output = field(:,:,:,:,:,list,:);  
        case 7,
            output = field(:,:,:,:,:,:,list); 
    end
end
